package com.sxm.yiwei.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sxm.yiwei.R;
import com.sxm.yiwei.beans.ServiceContent;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.x;

import java.util.List;

/**
 * Created by KuKa on 2017/7/6.
 * 搜索列表
 */

public class SearchGridAdapter extends BaseAdapter {
    Context context;
    public List<ServiceContent.DataBean.OtherServiceBean> gridlist;
    public SearchGridAdapter(Context context,  List<ServiceContent.DataBean.OtherServiceBean> gridlist) {
        this.context=context;
        this.gridlist=gridlist;
    }

    @Override
    public int getCount() {
        return gridlist.size();
    }

    @Override
    public Object getItem(int position) {
        return gridlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView==null){
            holder = new ViewHolder();
            view = View.inflate(context, R.layout.fwcontent_gridview,null);
            holder.griditem = (LinearLayout) view.findViewById(R.id.gridview_item);
            holder.gridimg = (ImageView) view.findViewById(R.id.content_gridimg);
            holder.gridtype = (TextView) view.findViewById(R.id.content_grid_type);
            holder.gridtypecontent = (TextView) view.findViewById(R.id.content_grid_typecontent);
            view.setTag(holder);
        }else {
            view=convertView;
            holder = (ViewHolder) view.getTag();
        }
        holder.gridtype.setText(gridlist.get(position).getName());
        int unit = gridlist.get(position).getUnit();
       switch (unit){
           case 1:
               holder.gridtypecontent.setText(gridlist.get(position).getAmount()+" 元/时");
               break;
           case 2:
               holder.gridtypecontent.setText(gridlist.get(position).getAmount()+" 元/天");
               break;
           case 3:
               holder.gridtypecontent.setText(gridlist.get(position).getAmount()+" 元/月");
               break;
           case 4:
               holder.gridtypecontent.setText(gridlist.get(position).getAmount()+" 元/单");
               break;
       }

        x.image().bind(holder.gridimg, InternetAddressUtis.IMG_URL+gridlist.get(position).getServiceAvatar());
        holder.griditem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickgridItemInf!=null){
                    onClickgridItemInf.OnClickGridItem(position);
                }
            }
        });
        notifyDataSetChanged();
        return view;
    }
    public class  ViewHolder{
        private ImageView gridimg;
        private TextView gridtype,gridtypecontent;
        private LinearLayout griditem;

    }
    private OnClickgridItemInf onClickgridItemInf;
    public interface OnClickgridItemInf{
        void OnClickGridItem(int p);
    }
    public void setOnGridItem(OnClickgridItemInf onClickgridItemInf){
        this.onClickgridItemInf = onClickgridItemInf;
    }

}
